import {
  VuexModule,
  Module,
  Mutation,
  Action,
  getModule,
} from 'vuex-module-decorators'
import store from '@/store'
interface IInnerState {
  id: number
  sendCount: number
  failedCount: number
  totalCount: number
  isStopped: boolean
}
export interface IRunningEmailTaskState {
  inner: IInnerState
}

@Module({ dynamic: true, store, name: 'runningEmailTask' })
class RunningEmailTask extends VuexModule implements IRunningEmailTaskState {
  inner: IInnerState = {
    id: 0,
    sendCount: 0,
    failedCount: 0,
    totalCount: 0,
    isStopped: false,
  }

  @Mutation
  private CHANGE_RUNNING_EMAIL_TASK(payload: IInnerState) {
    this.inner = payload
  }

  @Mutation
  private INCREASE_SEND_COUNT() {
    this.inner.sendCount++
  }
  @Mutation
  private INCREASE_FAILED_COUNT() {
    this.inner.failedCount++
  }

  @Mutation
  private CHANGE_STOP_STATE(state: boolean) {
    console.log(state, this.inner.isStopped)
    this.inner.isStopped = state
  }

  @Action
  public INIT_RUNNING_EMAIL_TASK(payload: IInnerState) {
    this.CHANGE_RUNNING_EMAIL_TASK(payload)
  }

  @Action
  public NEW_RUNNING_EMAIL_TASK(payload: {
    id: number
    totalCount: number
    sendCount?: number
    isStopped?: boolean
  }) {
    console.log(payload, 'totalCount')
    this.CHANGE_RUNNING_EMAIL_TASK({
      id: payload.id,
      sendCount: payload.sendCount || 0,
      totalCount: payload.totalCount,
      isStopped: payload.isStopped || false,
      failedCount: 0,
    })
    console.log(this)
  }

  @Action
  public IncreaseSendCount() {
    this.INCREASE_SEND_COUNT()
  }

  @Action
  public IncreaseFailedCount() {
    this.INCREASE_FAILED_COUNT()
  }

  @Action
  public PauseSendTask() {
    console.log(this)
    this.CHANGE_STOP_STATE(true)
  }

  @Action
  public ResumeSendTask() {
    console.log(this)
    this.CHANGE_STOP_STATE(false)
  }
}

export const RunningEmailTaskModule = getModule(RunningEmailTask)
